<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>gozerlib.utils.url &mdash; GOZERWAVE v0.1 BETA documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1 BETA',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="GOZERWAVE v0.1 BETA documentation" href="../../index.html" />
    <link rel="next" title="gozerlib.utils.web" href="web.html" />
    <link rel="prev" title="gozerlib.utils.trace" href="trace.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="web.html" title="gozerlib.utils.web"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="trace.html" title="gozerlib.utils.trace"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-gozerlib.utils.url">
<h1>gozerlib.utils.url<a class="headerlink" href="#module-gozerlib.utils.url" title="Permalink to this headline">¶</a></h1>
<p>url related functions.</p>
<dl class="class">
<dt id="gozerlib.utils.url.CBURLopener">
<em class="property">class </em><tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">CBURLopener</tt><big>(</big><em>version</em>, <em>*args</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.CBURLopener" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/urllib.html#urllib.FancyURLopener"><tt class="xref docutils literal"><span class="pre">urllib.FancyURLopener</span></tt></a></p>
<p>our URLOpener</p>
</dd></dl>

<dl class="class">
<dt id="gozerlib.utils.url.Stripper">
<em class="property">class </em><tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">Stripper</tt><a class="headerlink" href="#gozerlib.utils.url.Stripper" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/sgmllib.html#sgmllib.SGMLParser"><tt class="xref docutils literal"><span class="pre">sgmllib.SGMLParser</span></tt></a></p>
<p>html stripper.</p>
<dl class="method">
<dt id="gozerlib.utils.url.Stripper.handle_data">
<tt class="descname">handle_data</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.Stripper.handle_data" title="Permalink to this definition">¶</a></dt>
<dd>data handler.</dd></dl>

<dl class="method">
<dt id="gozerlib.utils.url.Stripper.strip">
<tt class="descname">strip</tt><big>(</big><em>some_html</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.Stripper.strip" title="Permalink to this definition">¶</a></dt>
<dd>strip html.</dd></dl>

</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.decode_html_entities">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">decode_html_entities</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.decode_html_entities" title="Permalink to this definition">¶</a></dt>
<dd>smart decoding of html entities to utf-8</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.deleteurl">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">deleteurl</tt><big>(</big><em>url</em>, <em>myheaders={}</em>, <em>postdata={}</em>, <em>keyfile=''</em>, <em>certfile=''</em>, <em>port=80</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.deleteurl" title="Permalink to this definition">¶</a></dt>
<dd>very basic HTTP DELETE.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.get_encoding">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">get_encoding</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.get_encoding" title="Permalink to this definition">¶</a></dt>
<dd>get encoding from web data</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.getpostdata">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">getpostdata</tt><big>(</big><em>event</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.getpostdata" title="Permalink to this definition">¶</a></dt>
<dd>retrive post data from url data.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.geturl">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">geturl</tt><big>(</big><em>url</em>, <em>version=None</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.geturl" title="Permalink to this definition">¶</a></dt>
<dd>fetch an url.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.geturl2">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">geturl2</tt><big>(</big><em>url</em>, <em>decode=False</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.geturl2" title="Permalink to this definition">¶</a></dt>
<dd>use urllib2 to fetch an url.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.geturl3">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">geturl3</tt><big>(</big><em>url</em>, <em>myheaders={}</em>, <em>postdata={}</em>, <em>keyfile=''</em>, <em>certfile=''</em>, <em>port=80</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.geturl3" title="Permalink to this definition">¶</a></dt>
<dd>stub .. NOT USED.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.geturl4">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">geturl4</tt><big>(</big><em>url</em>, <em>myheaders={}</em>, <em>postdata={}</em>, <em>keyfile=''</em>, <em>certfile=''</em>, <em>port=80</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.geturl4" title="Permalink to this definition">¶</a></dt>
<dd>use httplib to fetch an url.</dd></dl>

<dl class="class">
<dt id="gozerlib.utils.url.istr">
<em class="property">class </em><tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">istr</tt><a class="headerlink" href="#gozerlib.utils.url.istr" title="Permalink to this definition">¶</a></dt>
<dd>Bases: <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a></dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.posturl">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">posturl</tt><big>(</big><em>url</em>, <em>myheaders</em>, <em>postdata</em>, <em>keyfile=None</em>, <em>certfile=''</em>, <em>port=80</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.posturl" title="Permalink to this definition">¶</a></dt>
<dd>very basic HTTP POST url retriever.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.puturl">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">puturl</tt><big>(</big><em>url</em>, <em>myheaders={}</em>, <em>postdata={}</em>, <em>keyfile=''</em>, <em>certfile=''</em>, <em>port=80</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.puturl" title="Permalink to this definition">¶</a></dt>
<dd>very basic HTTP PUT.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.striphtml">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">striphtml</tt><big>(</big><em>txt</em><big>)</big><a class="headerlink" href="#gozerlib.utils.url.striphtml" title="Permalink to this definition">¶</a></dt>
<dd>strip html from txt.</dd></dl>

<dl class="function">
<dt id="gozerlib.utils.url.useragent">
<tt class="descclassname">gozerlib.utils.url.</tt><tt class="descname">useragent</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.utils.url.useragent" title="Permalink to this definition">¶</a></dt>
<dd>provide useragent string</dd></dl>

<div class="section" id="code">
<h2>code<a class="headerlink" href="#code" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335</pre></td><td class="code"><div class="highlight"><pre><span class="c"># gozerbot/utils/url.py</span>
<span class="c">#</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot; url related functions. &quot;&quot;&quot;</span>

<span class="c">## gozerlib imports</span>

<span class="kn">from</span> <span class="nn">generic</span> <span class="kn">import</span> <span class="n">fromenc</span>
<span class="kn">from</span> <span class="nn">gozerlib.config</span> <span class="kn">import</span> <span class="n">Config</span>

<span class="c">## basic imports</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">traceback</span>
<span class="kn">import</span> <span class="nn">Queue</span>
<span class="kn">import</span> <span class="nn">urllib</span>
<span class="kn">import</span> <span class="nn">urllib2</span>
<span class="kn">import</span> <span class="nn">urlparse</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sgmllib</span>
<span class="kn">import</span> <span class="nn">thread</span>
<span class="kn">import</span> <span class="nn">types</span>
<span class="kn">import</span> <span class="nn">httplib</span>
<span class="kn">import</span> <span class="nn">StringIO</span>
<span class="kn">import</span> <span class="nn">htmlentitydefs</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">import</span> <span class="nn">cgi</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">chardet</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">chardet</span> <span class="o">=</span> <span class="bp">None</span>

<span class="k">class</span> <span class="nc">istr</span><span class="p">(</span><span class="nb">str</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">def</span> <span class="nf">useragent</span><span class="p">():</span>

    <span class="sd">&quot;&quot;&quot; provide useragent string &quot;&quot;&quot;</span>

    <span class="n">config</span> <span class="o">=</span> <span class="n">Config</span><span class="p">(</span><span class="s">&#39;mainconfig&#39;</span><span class="p">)</span>
    <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">version</span><span class="p">)</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s">&#39;version&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mf">0</span><span class="p">:</span><span class="mf">2</span><span class="p">]</span>
    <span class="k">return</span> <span class="s">&#39;Mozilla/5.0 (compatible; </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">; http://www.gozerbot.org)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">version</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">CBURLopener</span><span class="p">(</span><span class="n">urllib</span><span class="o">.</span><span class="n">FancyURLopener</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; our URLOpener &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">version</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">version</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">=</span> <span class="n">version</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">=</span> <span class="n">useragent</span><span class="p">()</span>
        <span class="n">urllib</span><span class="o">.</span><span class="n">FancyURLopener</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">geturl</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; fetch an url. &quot;&quot;&quot;</span>

    <span class="n">urllib</span><span class="o">.</span><span class="n">_urlopener</span> <span class="o">=</span> <span class="n">CBURLopener</span><span class="p">(</span><span class="n">version</span><span class="p">)</span>
    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;fetching </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="n">tmp</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">result</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">tmp</span>

<span class="k">def</span> <span class="nf">geturl2</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">decode</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; use urllib2 to fetch an url. &quot;&quot;&quot;</span>

    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;fetching </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span>
    <span class="n">request</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="n">request</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s">&#39;User-Agent&#39;</span><span class="p">,</span> <span class="n">useragent</span><span class="p">())</span>
    <span class="n">opener</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">build_opener</span><span class="p">()</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
    <span class="n">tmp</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">info</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">info</span><span class="p">()</span> <span class="c"># add header information to .info attribute</span>
    <span class="n">result</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">decode</span><span class="p">:</span>
        <span class="n">encoding</span> <span class="o">=</span> <span class="n">get_encoding</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> encoding: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">encoding</span><span class="p">))</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">istr</span><span class="p">(</span><span class="n">fromenc</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="n">encoding</span><span class="p">,</span> <span class="n">url</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">istr</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
    <span class="n">res</span><span class="o">.</span><span class="n">info</span> <span class="o">=</span> <span class="n">info</span>
    <span class="k">return</span> <span class="n">res</span>

<span class="k">def</span> <span class="nf">geturl3</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">myheaders</span><span class="o">=</span><span class="p">{},</span> <span class="n">postdata</span><span class="o">=</span><span class="p">{},</span><span class="n">keyfile</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">certfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mf">80</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; stub .. NOT USED. &quot;&quot;&quot;</span>

    <span class="k">return</span> <span class="n">geturl2</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">geturl4</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">myheaders</span><span class="o">=</span><span class="p">{},</span> <span class="n">postdata</span><span class="o">=</span><span class="p">{},</span> <span class="n">keyfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">certfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mf">80</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; use httplib to fetch an url. &quot;&quot;&quot;</span>

    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;Content-Type&#39;</span><span class="p">:</span> <span class="s">&#39;text/html&#39;</span><span class="p">,</span> \
<span class="s">&#39;Accept&#39;</span><span class="p">:</span> <span class="s">&#39;text/plain; text/html&#39;</span><span class="p">,</span> <span class="s">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="n">useragent</span><span class="p">()}</span>
    <span class="n">headers</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">myheaders</span><span class="p">)</span>
    <span class="c"># parse URL components</span>
    <span class="n">urlparts</span> <span class="o">=</span> <span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
       <span class="n">port</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)[</span><span class="mf">1</span><span class="p">])</span>
       <span class="n">host</span> <span class="o">=</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)[</span><span class="mf">0</span><span class="p">]</span>
    <span class="k">except</span><span class="p">:</span>
       <span class="n">host</span> <span class="o">=</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span>

    <span class="c"># set up HTTP connection</span>

    <span class="k">if</span> <span class="n">keyfile</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">keyfile</span><span class="p">,</span> \
<span class="n">certfile</span><span class="p">)</span>
    <span class="k">elif</span> <span class="s">&#39;https&#39;</span> <span class="ow">in</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">0</span><span class="p">]:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span>

    <span class="c"># make the request</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span> <span class="o">==</span> <span class="n">types</span><span class="o">.</span><span class="n">DictType</span><span class="p">:</span>
        <span class="n">postdata</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span>

    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;fetching </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span>
    <span class="n">connection</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span>

    <span class="c"># read the response and clean up</span>
    <span class="k">return</span> <span class="n">connection</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">posturl</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">myheaders</span><span class="p">,</span> <span class="n">postdata</span><span class="p">,</span> <span class="n">keyfile</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">certfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span><span class="n">port</span><span class="o">=</span><span class="mf">80</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; very basic HTTP POST url retriever. &quot;&quot;&quot;</span>

    <span class="c"># build headers</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;Content-Type&#39;</span><span class="p">:</span> <span class="s">&#39;application/x-www-form-urlencoded&#39;</span><span class="p">,</span> \
<span class="s">&#39;Accept&#39;</span><span class="p">:</span> <span class="s">&#39;text/plain; text/html&#39;</span><span class="p">,</span> <span class="s">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="n">useragent</span><span class="p">()}</span>
    <span class="n">headers</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">myheaders</span><span class="p">)</span>

    <span class="c"># parse URL components</span>
    <span class="n">urlparts</span> <span class="o">=</span> <span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="c"># set up HTTP connection</span>
    <span class="k">if</span> <span class="n">keyfile</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span> <span class="n">port</span><span class="p">,</span> <span class="n">keyfile</span><span class="p">,</span> \
<span class="n">certfile</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span>

    <span class="c"># make the request</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span> <span class="o">==</span> <span class="n">types</span><span class="o">.</span><span class="n">DictType</span><span class="p">:</span>
        <span class="n">postdata</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span>

    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;fetching </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span>
    <span class="n">connection</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&#39;POST&#39;</span><span class="p">,</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">2</span><span class="p">],</span> <span class="n">postdata</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>

    <span class="c"># read the response and clean up</span>
    <span class="k">return</span> <span class="n">connection</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">deleteurl</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">myheaders</span><span class="o">=</span><span class="p">{},</span> <span class="n">postdata</span><span class="o">=</span><span class="p">{},</span> <span class="n">keyfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">certfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mf">80</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; very basic HTTP DELETE. &quot;&quot;&quot;</span>

    <span class="c"># build headers</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;Content-Type&#39;</span><span class="p">:</span> <span class="s">&#39;application/x-www-form-urlencoded&#39;</span><span class="p">,</span> \
<span class="s">&#39;Accept&#39;</span><span class="p">:</span> <span class="s">&#39;text/plain; text/html&#39;</span><span class="p">,</span> <span class="s">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="n">useragent</span><span class="p">()}</span>
    <span class="n">headers</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">myheaders</span><span class="p">)</span>

    <span class="c"># parse URL components</span>
    <span class="n">urlparts</span> <span class="o">=</span> <span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="c"># set up HTTP connection</span>
    <span class="k">if</span> <span class="n">keyfile</span> <span class="ow">and</span> <span class="n">certfile</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span> <span class="n">port</span><span class="p">,</span> <span class="n">keyfile</span><span class="p">,</span> \
<span class="n">certfile</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span>

    <span class="c"># make the request</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span> <span class="o">==</span> <span class="n">types</span><span class="o">.</span><span class="n">DictType</span><span class="p">:</span>
        <span class="n">postdata</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span>

    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;fetching </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span>
    <span class="n">connection</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&#39;DELETE&#39;</span><span class="p">,</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">2</span><span class="p">],</span> <span class="n">postdata</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>

    <span class="c"># read the response and clean up</span>
    <span class="k">return</span> <span class="n">connection</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">puturl</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">myheaders</span><span class="o">=</span><span class="p">{},</span> <span class="n">postdata</span><span class="o">=</span><span class="p">{},</span> <span class="n">keyfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">certfile</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mf">80</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; very basic HTTP PUT. &quot;&quot;&quot;</span>

    <span class="c"># build headers</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;Content-Type&#39;</span><span class="p">:</span> <span class="s">&#39;application/x-www-form-urlencoded&#39;</span><span class="p">,</span> \
<span class="s">&#39;Accept&#39;</span><span class="p">:</span> <span class="s">&#39;text/plain; text/html&#39;</span><span class="p">,</span> <span class="s">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="n">useragent</span><span class="p">()}</span>
    <span class="n">headers</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">myheaders</span><span class="p">)</span>

    <span class="c"># parse URL components</span>
    <span class="n">urlparts</span> <span class="o">=</span> <span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="c"># set up HTTP connection</span>
    <span class="k">if</span> <span class="n">keyfile</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span> <span class="n">port</span><span class="p">,</span> <span class="n">keyfile</span><span class="p">,</span> \
<span class="n">certfile</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">urlparts</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span>

    <span class="c"># make the request</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span> <span class="o">==</span> <span class="n">types</span><span class="o">.</span><span class="n">DictType</span><span class="p">:</span>
        <span class="n">postdata</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">postdata</span><span class="p">)</span>

    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;fetching </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span>
    <span class="n">connection</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&#39;PUT&#39;</span><span class="p">,</span> <span class="n">urlparts</span><span class="p">[</span><span class="mf">2</span><span class="p">],</span> <span class="n">postdata</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>

    <span class="c"># read the response and clean up</span>
    <span class="k">return</span> <span class="n">connection</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">getpostdata</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; retrive post data from url data. &quot;&quot;&quot;</span>

    <span class="n">ctype</span><span class="p">,</span> <span class="n">pdict</span> <span class="o">=</span> <span class="n">cgi</span><span class="o">.</span><span class="n">parse_header</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="s">&#39;content-type&#39;</span><span class="p">))</span>
    <span class="n">body</span> <span class="o">=</span> <span class="n">cgi</span><span class="o">.</span><span class="n">FieldStorage</span><span class="p">(</span><span class="n">fp</span><span class="o">=</span><span class="n">event</span><span class="o">.</span><span class="n">rfile</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">event</span><span class="o">.</span><span class="n">headers</span><span class="p">,</span> <span class="n">environ</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;REQUEST_METHOD&#39;</span><span class="p">:</span><span class="s">&#39;POST&#39;</span><span class="p">},</span> <span class="n">keep_blank_values</span> <span class="o">=</span> <span class="mf">1</span><span class="p">)</span>
    <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">dict</span><span class="p">(</span><span class="n">body</span><span class="p">):</span>
        <span class="n">result</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">body</span><span class="o">.</span><span class="n">getfirst</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">result</span>

<span class="k">def</span> <span class="nf">decode_html_entities</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; smart decoding of html entities to utf-8 &quot;&quot;&quot;</span>

    <span class="n">re_ent_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">u&#39;&amp;([^;]+);&#39;</span><span class="p">)</span>
    <span class="n">re_entn_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">u&#39;&amp;#([^;]+);&#39;</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">,</span> <span class="s">&#39;replace&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_entn</span><span class="p">(</span><span class="n">match</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; convert to entities &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">htmlentitydefs</span><span class="o">.</span><span class="n">entitydefs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">1</span><span class="p">)):</span>
            <span class="k">return</span> <span class="n">htmlentitydefs</span><span class="o">.</span><span class="n">entitydefs</span><span class="p">[</span><span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">1</span><span class="p">)]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">,</span> \
<span class="s">&#39;replace&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_utf8</span><span class="p">(</span><span class="n">match</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; convert to utf-8 &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">unichr</span><span class="p">(</span><span class="nb">long</span><span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">1</span><span class="p">)))</span>

    <span class="n">s</span> <span class="o">=</span> <span class="n">re_ent_match</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">to_entn</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">re_entn_match</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">to_utf8</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>

<span class="k">def</span> <span class="nf">get_encoding</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; get encoding from web data &quot;&quot;&quot;</span>

    <span class="c"># first we try if we have the .info attribute to determine the encoding from</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="s">&#39;info&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">data</span><span class="o">.</span><span class="n">info</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;content-type&#39;</span><span class="p">)</span> <span class="ow">and</span> \
<span class="s">&#39;charset&#39;</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">info</span><span class="p">[</span><span class="s">&#39;content-type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">():</span>
        <span class="n">charset</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">info</span><span class="p">[</span><span class="s">&#39;content-type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;charset&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)[</span><span class="mf">1</span><span class="p">]</span><span class="o">.</span>\
<span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">charset</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;=&#39;</span><span class="p">:</span>
            <span class="n">charset</span> <span class="o">=</span> <span class="n">charset</span><span class="p">[</span><span class="mf">1</span><span class="p">:]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="s">&#39;;&#39;</span> <span class="ow">in</span> <span class="n">charset</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">charset</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;;&#39;</span><span class="p">)[</span><span class="mf">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">charset</span>

    <span class="c"># try to find the charset in the meta tags, </span>
    <span class="c"># &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=...&quot; /&gt;</span>
    <span class="k">if</span> <span class="s">&#39;&lt;meta&#39;</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">lower</span><span class="p">():</span>
        <span class="n">metas</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">u&#39;&lt;meta[^&gt;]+&gt;&#39;</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">I</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">M</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">metas</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">meta</span> <span class="ow">in</span> <span class="n">metas</span><span class="p">:</span>
                <span class="n">test_http_equiv</span> <span class="o">=</span> \
<span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">&#39;http-equiv\s*=\s*[</span><span class="se">\&#39;</span><span class="s">&quot;]([^</span><span class="se">\&#39;</span><span class="s">&quot;]+)[</span><span class="se">\&#39;</span><span class="s">&quot;]&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">I</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">test_http_equiv</span> <span class="ow">and</span> \
<span class="n">test_http_equiv</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;content-type&#39;</span><span class="p">:</span>
                    <span class="n">test_content</span> <span class="o">=</span> \
<span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">&#39;content\s*=\s*[</span><span class="se">\&#39;</span><span class="s">&quot;]([^</span><span class="se">\&#39;</span><span class="s">&quot;]+)[</span><span class="se">\&#39;</span><span class="s">&quot;]&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">I</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">test_content</span><span class="p">:</span>
                        <span class="n">test_charset</span> <span class="o">=</span> \
<span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">&#39;charset\s*=\s*([^\s</span><span class="se">\&#39;</span><span class="s">&quot;]+)&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">I</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">test_charset</span><span class="p">:</span>
                            <span class="k">return</span> <span class="n">test_charset</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span>

    <span class="c"># everything else failed, let&#39;s see if we can use chardet</span>
    <span class="k">if</span> <span class="n">chardet</span><span class="p">:</span>
        <span class="n">test</span> <span class="o">=</span> <span class="n">chardet</span><span class="o">.</span><span class="n">detect</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">test</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;encoding&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">test</span><span class="p">[</span><span class="s">&#39;encoding&#39;</span><span class="p">]</span>

    <span class="c"># nothing found, let&#39;s fall back to the default encoding</span>
    <span class="k">return</span> <span class="n">sys</span><span class="o">.</span><span class="n">getdefaultencoding</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Stripper</span><span class="p">(</span><span class="n">sgmllib</span><span class="o">.</span><span class="n">SGMLParser</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; html stripper. &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">sgmllib</span><span class="o">.</span><span class="n">SGMLParser</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">strip</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">some_html</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; strip html. &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">theString</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">some_html</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">theString</span>

    <span class="k">def</span> <span class="nf">handle_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; data handler. &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">theString</span> <span class="o">+=</span> <span class="n">fromenc</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">striphtml</span><span class="p">(</span><span class="n">txt</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; strip html from txt. &quot;&quot;&quot;</span>

    <span class="n">stripper</span> <span class="o">=</span> <span class="n">Stripper</span><span class="p">()</span>
    <span class="n">txt</span> <span class="o">=</span> <span class="n">stripper</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="n">fromenc</span><span class="p">(</span><span class="n">txt</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">txt</span>
    
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">gozerlib.utils.url</a><ul>
<li><a class="reference external" href="#code">code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="trace.html"
                                  title="previous chapter">gozerlib.utils.trace</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="web.html"
                                  title="next chapter">gozerlib.utils.web</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../../_sources/gozerlib/utils/url.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="web.html" title="gozerlib.utils.web"
             >next</a> |</li>
        <li class="right" >
          <a href="trace.html" title="gozerlib.utils.trace"
             >previous</a> |</li>
        <li><a href="../../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Bart Thate.
      Last updated on Nov 14, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>